package Ep05_StackAndQueue._1047;

import java.util.Stack;

/**
 * @author Jimmy Zhan WORKSTATION
 * @date 2023/4/13 19:13
 * 说明：使用传统的栈解决匹配问题
 */

public class Mine {
    public String removeDuplicates(String s) {
        String res = "";
        Stack<Character> chars = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            // 优先判断栈空以及与栈顶不相同的情况，push进去
            if (chars.isEmpty() || ch != chars.peek()){
                chars.push(ch);
            } else {
                // 否则pop
                chars.pop();
            }
        }
        while (!chars.isEmpty()) {
            res = chars.pop() + res;
        }
        return res;
    }
}
